<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Go Crontab | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Go Crontab</h1>
<p>[TOC]</p>
<h1 id="计划任务">计划任务</h1>
<p>from Godoc</p>
<p>包cron实现了cron规范解析器和作业运行器。</p>
<p>安装
要下载特定的标记版本，请运行:</p>
<p><code>go get github.com/robfig/cron/v3@v3.0.0</code>
将其导入您的程序中:</p>
<p><code>import “github.com/robfig/cron/v3”</code>
由于Go Modules的使用，它需要Go 1.11或更高版本。</p>
<p>用法
呼叫者可以在给定的时间表上注册要调用的Func。Cron将在他们自己的goroutines中运行它们。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">c</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>()
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;30 * * * *&#34;</span>, <span style="color:#66d9ef">func</span>() { <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Every hour on the half hour&#34;</span>) })
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;30 3-6,20-23 * * *&#34;</span>, <span style="color:#66d9ef">func</span>() { <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;.. in the range 3-6am, 8-11pm&#34;</span>) })
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;CRON_TZ=Asia/Tokyo 30 04 * * * *&#34;</span>, <span style="color:#66d9ef">func</span>() { <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Runs at 04:30 Tokyo time every day&#34;</span>) })
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;@hourly&#34;</span>,      <span style="color:#66d9ef">func</span>() { <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Every hour, starting an hour from now&#34;</span>) })
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;@every 1h30m&#34;</span>, <span style="color:#66d9ef">func</span>() { <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Every hour thirty, starting an hour thirty from now&#34;</span>) })
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Start</span>()
</span></span><span style="display:flex;"><span>..
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Funcs are invoked in their own goroutine, asynchronously.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Funcs may also be added to a running Cron
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;@daily&#34;</span>, <span style="color:#66d9ef">func</span>() { <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Every day&#34;</span>) })
</span></span><span style="display:flex;"><span>..
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Inspect the cron job entries&#39; next and previous run times.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">inspect</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Entries</span>())
</span></span><span style="display:flex;"><span>..
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Stop</span>()  <span style="color:#75715e">// Stop the scheduler (does not stop any jobs already running).
</span></span></span></code></pre></div><h3 id="cron表达格式">CRON表达格式</h3>
<p>cron表达式表示一组时间，使用5个以空格分隔的字段。</p>
<table>
<thead>
<tr>
<th>字段名称</th>
<th>强制性？</th>
<th>允许值</th>
<th>允许特殊字符</th>
</tr>
</thead>
<tbody>
<tr>
<td>分钟</td>
<td>是的</td>
<td>0-59</td>
<td>* /， -</td>
</tr>
<tr>
<td>小时</td>
<td>是的</td>
<td>0-23</td>
<td>* /， -</td>
</tr>
<tr>
<td>一个月的某一天</td>
<td>是的</td>
<td>1-31</td>
<td>* /， - ？</td>
</tr>
<tr>
<td>月</td>
<td>是的</td>
<td>1-12或JAN-DEC</td>
<td>* /， -</td>
</tr>
<tr>
<td>星期几</td>
<td>是的</td>
<td>0-6或SUN-SAT</td>
<td>* /， - ？</td>
</tr>
<tr>
<td>月份和星期几字段值不区分大小写。“SUN”，“Sun”和“sun”同样被接受。</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>该格式的具体解释基于Cron维基百科页面：https://en.wikipedia.org/wiki/Cron</p>
<h3 id="替代格式">替代格式</h3>
<p><strong>备用Cron表达式格式支持其他字段，如秒。您可以通过创建自定义Parser来实现，如下所示。</strong></p>
<p>cron.New（
cron.WithParser（
cron.NewParser（
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor）））
**由于添加Seconds是对标准cron规范的最常见修改，cron提供了一个内置函数来执行此操作，这相当于您之前看到的自定义解析器，除了它的秒字段是必需的: **</p>
<p>cron.New（cron.WithSeconds（））
这模仿Quartz，最流行的替代Cron计划格式：http://www.quartz-scheduler.org/documentation/2.4.0-SNAPSHOT/tutorials/tutorial-lesson-06.html</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span> <span style="color:#75715e"># ┌───────────── min (0 - 59)</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ ┌────────────── hour (0 - 23)</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ │ ┌─────────────── day of month (1 - 31)</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ │ │ ┌──────────────── month (1 - 12)</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ │ │ │ │                  Saturday, or use names; 7 is also Sunday)</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ │ │ │ │</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># │ │ │ │ │</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e"># * * * * *  command to execute</span>
</span></span></code></pre></div><h2 id="cron特定字符说明">cron特定字符说明</h2>
<p>特殊字符
星号（*）</p>
<p>星号表示cron表达式将匹配该字段的所有值; 例如，在第5个字段（月）中使用星号表示每个月。</p>
<p>斜线（/）</p>
<p>斜杠用于描述范围的增量。例如，第1场（分钟）中的3-59 / 15表示小时的第3分钟，之后每15分钟。形式“* \ / &hellip;”等同于“first-last / &hellip;”形式，即在该字段的最大可能范围内的增量。形式“N / &hellip;”被接受为“N-MAX / &hellip;”，即从N开始，使用增量直到该特定范围的结束。它没有环绕。</p>
<p>逗号（，）</p>
<p>逗号用于分隔列表的项目。例如，在第5个字段（星期几）中使用“MON，WED，FRI”将表示星期一，星期三和星期五。</p>
<p>连字符（ - ）</p>
<p>连字符用于定义范围。例如，9-17表示每天上午9点到下午5点之间的每小时。</p>
<p>问号（？）</p>
<p>可以使用问号而不是&rsquo;*&lsquo;来留下日期或星期几的空白。</p>
<h4 id="预定义的时间表">预定义的时间表</h4>
<p>您可以使用几个预定义的计划之一来代替cron表达式。</p>
<p>进入| 说明| 相当于
&mdash;&ndash; | &mdash;&mdash;&mdash;&ndash; &mdash;&mdash;&mdash;&mdash;-
@yearly（或@annually）| 每年一次，午夜，1月1日| 0 0 1 1 *
@monthly | 每月运行一次，午夜，月初| 0 0 1 * *
@weekly | 每周一次，午睡/周日午夜| 0 0 * * 0
@daily（或@midnight）| 每天运行一次，午夜| 0 0 * * *
@hourly | 每小时运行一次，小时开始| 0 * * * *</p>
<h4 id="间隔">间隔</h4>
<p>您还可以安排作业以固定的时间间隔执行，从添加或运行cron开始。这可以通过格式化cron规范来支持，如下所示:</p>
<p>@every <!-- raw HTML omitted -->
其中“duration”是time.ParseDuration（http://golang.org/pkg/time/#ParseDuration）接受的字符串。</p>
<p>例如，“@ every 1h30m10s”表示在1小时30分10秒之后激活的计划，然后是之后的每个间隔。</p>
<p>注意: 间隔不会考虑作业运行时。例如，如果作业需要3分钟才能运行，并且计划每5分钟运行一次，则每次运行之间只有2分钟的空闲时间。</p>
<h3 id="cron举例说明">cron举例说明</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">每隔1分钟执行一次</span>:  <span style="color:#f92672">*/</span><span style="color:#ae81ff">1</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#960050;background-color:#1e0010">?</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">每天23点执行一次</span>:  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">23</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#960050;background-color:#1e0010">?</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">每天凌晨1点执行一次</span>:  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">1</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#960050;background-color:#1e0010">?</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">每月1号凌晨1点执行一次</span>:  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span> <span style="color:#f92672">*</span> <span style="color:#960050;background-color:#1e0010">?</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">在26分</span><span style="color:#960050;background-color:#1e0010">、</span><span style="color:#ae81ff">29</span><span style="color:#a6e22e">分</span><span style="color:#960050;background-color:#1e0010">、</span><span style="color:#ae81ff">33</span><span style="color:#a6e22e">分执行一次</span>:  <span style="color:#ae81ff">26</span>,<span style="color:#ae81ff">29</span>,<span style="color:#ae81ff">33</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#960050;background-color:#1e0010">?</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">每天的0点</span><span style="color:#960050;background-color:#1e0010">、</span><span style="color:#ae81ff">13</span><span style="color:#a6e22e">点</span><span style="color:#960050;background-color:#1e0010">、</span><span style="color:#ae81ff">18</span><span style="color:#a6e22e">点</span><span style="color:#960050;background-color:#1e0010">、</span><span style="color:#ae81ff">21</span><span style="color:#a6e22e">点都执行一次</span>:  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">13</span>,<span style="color:#ae81ff">18</span>,<span style="color:#ae81ff">21</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span> <span style="color:#960050;background-color:#1e0010">?</span>
</span></span></code></pre></div><h4 id="时区">时区</h4>
<p>默认情况下，所有解释和计划都在计算机的本地时区（time.Local）中完成。您可以在构造中指定不同的时区:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">WithLocation</span>(<span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">UTC</span>))
</span></span></code></pre></div><p>单个cron调度还可以通过在cron规范的开头提供<code>CRON_TZ=Asia/Tokyo</code>.覆盖它们将被解释的时区。</p>
<p>例如:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">Runs</span> <span style="color:#a6e22e">at</span> <span style="color:#ae81ff">6</span><span style="color:#a6e22e">am</span> <span style="color:#a6e22e">in</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Local</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>().<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;0 6 * * ?&#34;</span>, <span style="color:#f92672">...</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">Runs</span> <span style="color:#a6e22e">at</span> <span style="color:#ae81ff">6</span><span style="color:#a6e22e">am</span> <span style="color:#a6e22e">in</span> <span style="color:#a6e22e">America</span><span style="color:#f92672">/</span><span style="color:#a6e22e">New_York</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">nyc</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">LoadLocation</span>(<span style="color:#e6db74">&#34;America/New_York&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>(<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">WithLocation</span>(<span style="color:#a6e22e">nyc</span>))
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;0 6 * * ?&#34;</span>, <span style="color:#f92672">...</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">Runs</span> <span style="color:#a6e22e">at</span> <span style="color:#ae81ff">6</span><span style="color:#a6e22e">am</span> <span style="color:#a6e22e">in</span> <span style="color:#a6e22e">Asia</span><span style="color:#f92672">/</span><span style="color:#a6e22e">Tokyo</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>().<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;CRON_TZ=Asia/Tokyo 0 6 * * ?&#34;</span>, <span style="color:#f92672">...</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">Runs</span> <span style="color:#a6e22e">at</span> <span style="color:#ae81ff">6</span><span style="color:#a6e22e">am</span> <span style="color:#a6e22e">in</span> <span style="color:#a6e22e">Asia</span><span style="color:#f92672">/</span><span style="color:#a6e22e">Tokyo</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>(<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">WithLocation</span>(<span style="color:#a6e22e">nyc</span>))
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">SetLocation</span>(<span style="color:#e6db74">&#34;America/New_York&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">AddFunc</span>(<span style="color:#e6db74">&#34;CRON_TZ=Asia/Tokyo 0 6 * * ?&#34;</span>, <span style="color:#f92672">...</span>)
</span></span></code></pre></div><p>传统兼容性还支持前缀“TZ =(TIME ZONE)”。</p>
<p>请注意，在夏令时跨越式过渡期间安排的作业将无法运行！</p>
<p>作业包装机/链条</p>
<p>Cron运行器可以配置一系列作业包装器，以便为所有提交的作业添加横切功能。例如，它们可用于实现以下效果:</p>
<ul>
<li>从作业中恢复任何恐慌（默认激活）</li>
<li>如果先前的运行尚未完成，则延迟作业的执行</li>
<li>如果之前的运行尚未完成，则跳过作业的执行</li>
<li>记录每个作业的调用
使用<code>cron.WithChain</code>选项为添加到cron的所有作业安装包装器:</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>(<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">WithChain</span>(
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">SkipIfStillRunning</span>(<span style="color:#a6e22e">logger</span>),
</span></span><span style="display:flex;"><span>))
</span></span></code></pre></div><p>通过显式包装它们来为各个作业安装包装器:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">job</span> = <span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">NewChain</span>(
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">SkipIfStillRunning</span>(<span style="color:#a6e22e">logger</span>),
</span></span><span style="display:flex;"><span>).<span style="color:#a6e22e">Then</span>(<span style="color:#a6e22e">job</span>)
</span></span></code></pre></div><p>线程安全
由于Cron服务与调用代码同时运行，因此必须采取一些措施以确保正确同步。</p>
<p>只要调用者确保调用在它们之间进行排序之前有明确的发生，所有cron方法都被设计为正确同步。</p>
<p>记录
Cron定义了一个Logger接口，它是github.com/go-logr/logr中定义的接口的一个子集。它有两个日志记录级别（信息和错误），参数是键/值对。这使得cron日志记录可以插入结构化日志记录系统。提供了一个适配器[Verbose] PrintfLogger来包装标准库* log.Logger。</p>
<p>为了进一步了解Cron操作，可以激活详细日志记录，这将记录作业运行，调度决策以及添加或删除的作业。使用一次性记录器激活它，如下所示:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">New</span>(
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">WithLogger</span>(
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">cron</span>.<span style="color:#a6e22e">VerbosePrintfLogger</span>(<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">New</span>(<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Stdout</span>, <span style="color:#e6db74">&#34;cron: &#34;</span>, <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">LstdFlags</span>))))
</span></span></code></pre></div><p>履行
Cron条目存储在一个数组中，按其下一个激活时间排序。Cron睡着，直到下一份工作开始运行。</p>
<p>醒来后:</p>
<ul>
<li>它运行在该秒钟上处于活动状态的每个条目</li>
<li>它计算运行的作业的下一个运行时间</li>
<li>它按下一个激活时间重新排序条目数组。</li>
<li>它会一直睡到最快的工作。</li>
</ul>

    </article>
  </div>

  
  
  

  
</body>

</html>